package com.jeasonzhao.report.exceptions;

import com.jeasonzhao.report.model.ReportDatabaseConfiguration;
import com.jeasonzhao.report.msg.Message;

public class DatabaseException extends SQLReportException
{
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public DatabaseException(Object ...objs)
    {
        super(objs);
    }

    public DatabaseException(Throwable t,Object ...objs)
    {
        super(t,objs);
    }

    protected static String getDesc(ReportDatabaseConfiguration db)
    {
        if(null == db)
        {
            return "[Unknown Dictionary Defination]";
        }
        else
        {
            return db.getDatabaseName() + ":" + db.getDatabaseId() + "(" + db.getConfigType() + ":" + db.getConfigSrc() + ")";
        }
    }

    @Message(
        {"zh_cn","{0}无数据库连接Id"
        ,"en","{0}No identifier input"})
    public static class NoIdentifier extends DatabaseException
    {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        public NoIdentifier(ReportDatabaseConfiguration db)
        {
            super(DatabaseException.getDesc(db));
        }
    }

    @Message(
        {"zh_cn","{0}找不到对应的数据库连接Id"
        ,"en","{0}No identifier found in the repository"})
    public static class WrongIdentifier extends DatabaseException
    {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        public WrongIdentifier(ReportDatabaseConfiguration db)
        {
            super(DatabaseException.getDesc(db));
        }

        public WrongIdentifier(String strid)
        {
            super(strid);
        }

    }

    @Message(
        {"zh_cn","{0}不能连接到数据库Driver={1},URL={2}"
        ,"en","{0}Could not connect to database, Driver={1},URL={2}"})
    public static class ConnectFail extends DatabaseException
    {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        public ConnectFail(ReportDatabaseConfiguration db,Throwable e)
        {
            super(e,DatabaseException.getDesc(db)
                  ,null == db ? "" : db.getDriver(),null == db ? "" : db.getUrl()
                );
        }

    }

    @Message(
        {"zh_cn","{0}配置的类{1}不正确，类不能转换为{2}"
        ,"en","{0} Configuration Error, can not convert the class {1} to {2}"
    })
    public static class CastError extends DatabaseException
    {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        public CastError(ReportDatabaseConfiguration item,String strClassName,Class<?> destinationClass)
        {
            super(getDesc(item),strClassName
                  ,null == destinationClass ? "[Unknown destination]" : destinationClass.getName());
        }

        public CastError(ReportDatabaseConfiguration item,String strClassName,Class<?> destinationClass,Throwable t)
        {
            super(t,getDesc(item),strClassName
                  ,null == destinationClass ? "[Unknown destination]" : destinationClass.getName());
        }

    }

    @Message(
        {"zh_cn","{0}没有配置生成数据的URL"
        ,"en","{0} no URL has been set"
    })
    public static class NoURL extends DatabaseException
    {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        public NoURL(ReportDatabaseConfiguration dict)
        {
            super(getDesc(dict));
        }
    }

    @Message(
        {"zh_cn","{0}没有配置生成数据的Driver"
        ,"en","{0} no Driver has been set"
    })
    public static class NoDriver extends DatabaseException
    {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        public NoDriver(ReportDatabaseConfiguration dict)
        {
            super(getDesc(dict));
        }
    }

    @Message(
        {"zh_cn","{0}没有配置生成数据的URL,{1}"
        ,"en","{0} no URL has been set,{1}"
    })
    public static class URLBuildException extends DatabaseException
    {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        public URLBuildException(ReportDatabaseConfiguration dict,Throwable ex)
        {
            super(ex,getDesc(dict),ex.getMessage());
        }
    }

    @Message(
        {"zh_cn","{0}类{1}没有找到,{2}"
        ,"en","{0} Class {1} Not Found,{2}"
    })
    public static class ClassNotFound extends DatabaseException
    {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        public ClassNotFound(ReportDatabaseConfiguration dict,String strClassName,Throwable ex)
        {
            super(ex,getDesc(dict),strClassName,ex.getMessage());
        }
    }

}
